﻿#include "csetting.h"
#include "ui_csetting.h"
#include <QFileDialog>
#include <QDebug>
#include <xml/cxmlparser.h>

stSetting CSetting::_settingInstance;

CSetting::CSetting(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CSetting)
{
    ui->setupUi(this);

    QString filePath = QCoreApplication::applicationDirPath() + "/" + FILE_NAME_SETTING;
    _settingInstance = CXMLParser::parseSettingXMLFile(filePath);

    ui->edt_importQuesDir->setText(_settingInstance.dir_baseQuesLib);
    ui->edt_savedDir->setText(_settingInstance.dir_exportQues);

}

CSetting::~CSetting()
{
    delete ui;
}

stSetting& CSetting::getSettingInstance()
{
    QString filePath = QCoreApplication::applicationDirPath() + "/" + FILE_NAME_SETTING;
    if (_settingInstance.dir_baseQuesLib == "")
    {
        _settingInstance = CXMLParser::parseSettingXMLFile(filePath);
    }
    return _settingInstance;
}

void CSetting::on_btn_getImportDir_clicked()
{
    QString str = QFileDialog::getExistingDirectory(this, QString::fromLocal8Bit("选择题库所在目录"),  QString::fromLocal8Bit(""), NULL);
    if (str.isNull())
    {
        // 用户点击了取消
    }
    else
        // 选择了一个目录
    {
        ui->edt_importQuesDir->setText(str);
    }

}

void CSetting::on_btn_save_clicked()
{
    stSetting setting;
    setting.dir_baseQuesLib = ui->edt_importQuesDir->text();
    setting.dir_exportQues = ui->edt_savedDir->text();

    CXMLParser::reWriteSettingFile(setting, QCoreApplication::applicationDirPath() + "/" + FILE_NAME_SETTING);
}


void CSetting::on_btn_getExportDir_clicked()
{
    QString str = QFileDialog::getExistingDirectory(this, QString::fromLocal8Bit("题目导出到"),  QString::fromLocal8Bit(""), NULL);
    if (! str.isNull())
    {
        ui->edt_savedDir->setText(str);
    }
}
